@extends('admin.master')

@section('style')
	<link rel="stylesheet" href="{{ url('js/prettyphoto/css/prettyPhoto.css') }}">
@endsection

@section('top')
<div class="top-breadcrumb pull-left">{!! Breadcrumbs::render() !!}</div>
@endsection

@section('content')
<div class="container-body">
	{!! Form::open(['route' => 'admin.renew.store', 'class'=>'form-horizontal']) !!}
		<input type="hidden" name="order_id" value="{{ $order->id }}">
		<input type="hidden" name="referer" value="{{ request()->header("referer") }}">
		<div class="form-group">
			<label class="col-sm-4 control-label label-width">客户名称：</label>
			<div class="col-sm-8 row">
				<p class="form-control-static">{{ $order->customer->name }}</p>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-4 control-label label-width">年限：</label>
			<div class="col-sm-8 row">
				<p class="form-control-static">{{ $order->year }}</p>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-4 control-label label-width">订单开始日期：</label>
			<div class="col-sm-8 row">
				<p class="form-control-static">{{ $order->actualed_at->format('Y-m-d') }}</p>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-4 control-label label-width">到期日期：</label>
			<div class="col-sm-8 row">
				<p class="form-control-static">{{ $order->expired_at->format('Y-m-d') }}</p>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-4 control-label label-width"><span class="text-danger">*</span> 续费年限：</label>
			<div class="col-sm-8 row">
				{!! Form::selectRange('year', 1, 10, null, ['class'=>'form-control len100']) !!}
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-4 control-label label-width"><span class="text-danger">*</span> 业务人员：</label>
			<div class="col-sm-8 row">
				<div class="department-select" id="department">
					<select id="region" class="form-control len150 region"></select>
					<select id="department" class="form-control len150 department"></select>
					<select id="saler" name="saler_id" class="form-control autosize saler"></select>	
				</div>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-4 control-label label-width"><span class="text-danger">*</span> 续费金额：</label>
			<div class="col-sm-8 row">
				{!! Form::text('amount', null, ['class'=>'form-control len']) !!}
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-4 control-label label-width"><span class="text-danger">*</span> 合同号：</label>
			<div class="col-sm-8 row">
				{!! Form::text('contract_no', null, ['class'=>'form-control len']) !!}
				<div class="contract-no-tip">
					<span class="contract-no-info">可用合同号：</span>
					<p class="contract-no-list label-list">
						<img src="{{ url('images/loading-1.gif') }}">
					</p>
				</div>
				<a href="javascript:;" class="order-contract-add"><i class="fa fa-plus" aria-hidden="true"></i> 添加合同</a>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-4 control-label label-width"></label>
			<div class="col-sm-8 row">
				{!! Form::submit('提交续费', ['class'=>'btn btn-primary']) !!}
			</div>
		</div>
	{!! Form::close() !!}
</div>
<!-- Modal -->
<div class="modal fade" id="dialog-form-ajax" tabindex="-1" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="true"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">提示</h4>
        </div>
        <div class="modal-body clearfix">
            
        </div>
        <div class="modal-footer">
            <a class="btn btn-primary btn-modal-ok">确定</a>
            <button class="btn btn-default btn-modal-cancel btn-modal-close" data-dismiss="modal">取消</button>
        </div>
    </div>
  </div>
</div>
<!-- Modal End -->
@endsection

@section('foot')
<script src="{{ url('js/department.js') }}"></script>
<script src="{{ url('js/nice-validator/jquery.validator.min.js?local=zh-CN') }}"></script>
<script src="{{ url('js/prettyphoto/jquery.prettyPhoto.js') }}"></script>
<script type="text/javascript">
	var departments = {!! json_encode($departmentsTree, JSON_UNESCAPED_UNICODE) !!};
	var salers = {!! json_encode($salers, JSON_UNESCAPED_UNICODE) !!};

	department.init({
		regions: departments,
		departments: departments,
		salers: salers,
		where: {}
	}).make();

    // 异步获取可能的合同号
	$.ajax({
	   type: "GET",
	   url: HOME_URL + "/admin/contract/customer/{{ $order->customer->id }}",
	   success: function(data) {
	   		var html = [];
	   		var time = '';
			$.each(data, function(key, value) {
				time = value.created_at.split(' ');
				html.push('<a href="javascript:;"><span>'+value.contract_no+'</span></a>');
			});	
			$('.contract-no-list').html(html.join(''));
	   }
	});

$('.contract-no-list').on('click', 'a', function() {
	$("input[name='contract_no']").val($(this).find('span').text());
});

// 添加合同
var contractUrl = HOME_URL + '/admin/contract/create?pop=1';
$.get(contractUrl, function(data) {
	$('#dialog-form-ajax').find('.modal-body').html(data);
});
$('.order-contract-add').on('click', function(e) {
	e.preventDefault();
	$('#dialog-form-ajax').modal('show');
	return false;
});

// Ajax 提交表单
$('#dialog-form-ajax').on('click', '.btn-modal-ok', function() {
	var form = $(this).parents('.modal').find('form');
	form.trigger("validate");
});

$('#dialog-form-ajax').on('valid.form', function(e, form) {
	var url  = $(form).attr('action');
	$.ajax({
		type: "POST",
		url: url,
		data: $(form).serialize(),
		success: function(data) {
			if (data != '') {
				$("input[name='contract_no']").val(data);
			}
			$('#dialog-form-ajax').modal('hide');
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			if (XMLHttpRequest.status == 422) {
				swal("错误!", XMLHttpRequest.responseJSON.contract_no, "error");
				return false;	
			}
			swal("错误!", '服务器错误。', "error");	
			// console.log(JSON.stringify(XMLHttpRequest)); 
		}
	});
});
</script>
@endsection